package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.dao.LeaveDAOException;
import data.dto.LeaveDTO;
import data.dto.UserDTO;
import data.dto.UserHierarchyDTO;
import data.searchdto.LeaveSearchDTO;
import data.searchdto.UserHierarchyDetailSearchDTO;
import data.searchdto.UserHierarchySearchDTO;
import manager.LeaveManager;
import manager.UserManager;

/**
 * Servlet implementation class ViewStaffLeaveServlet
 */
@WebServlet("/ViewLeaveServlet")
public class ViewLeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewLeaveServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param leave
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			processRequest(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws LeaveDAOException,
			ServletException, IOException, SQLException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			LeaveManager leavemgr = new LeaveManager();
			UserManager userManager = new UserManager();
			ArrayList<LeaveDTO> leavedata = leavemgr.findAllLeave();

			Short type = null;
			if (null != request.getParameter("status")) {
				type = Short.parseShort(request.getParameter("status"));
			} else {
				type = util.Constants.LEAVE_APPLICATION_STATUS_ALL;
			}

			UserHierarchySearchDTO userHierarchySearch = new UserHierarchySearchDTO();
			userHierarchySearch.setUserType(util.CurrentUser.getCurrentUser(
					request.getSession()).getUserType());

			UserHierarchyDTO userHierarchy = userManager
					.findUserHierarchy(userHierarchySearch);

			if (userHierarchy != null) {

				UserHierarchyDetailSearchDTO userHierarchyDetailSearch = new UserHierarchyDetailSearchDTO();
				userHierarchyDetailSearch.setHierarchyId(userHierarchy.getId());

				 //if (type == util.Constants.LEAVE_APPLICATION_STATUS_ALL) {
				leavedata = leavemgr.findLeavebyUsers(type, userManager
						.findAllHierarchyDetail(userHierarchyDetailSearch));
				 //}

				
//				  else { LeaveSearchDTO search = new LeaveSearchDTO();
//				  search.setLeaveType((short) 0); search.setEndDate(null);
//				  search.setStartDate(null); search.setStatus(type); leavedata
//				  = leavemgr.findLeave(search); }
				 

				request.setAttribute("status", type);
				request.setAttribute("leaves", leavedata);
			}
			page = "/staff/viewStaffLeave.jsp";
		}
		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);

	}
}
